<template>
	<div class="system-score-container standard-container layout-padding layout-pd">
		<div class="review-list-title">
			<div class="title-actions">
				<el-button size="default" type="primary" class="ml10" @click="getTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="primary" class="ml10" @click="onOpen('add', '')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增积分商品
				</el-button>
				<!-- <el-row :gutter="10">
					<el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
						<el-form-item label="活动名称:">
							<el-input v-model="state.search.name" size="default" clearable placeholder="请输入活动名称" style="width: 100%"></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" v-show="state.isToggle">
						<el-form-item label="创建时间:">
							<el-date-picker
								type="datetimerange"
								v-model="state.search.searchDateTime"
								start-placeholder="创建开始时间"
								end-placeholder="创建时间"
								style="width: 100%"
							/>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="8">
						<el-form-item class="table-form-btn" label-width="10px">
							<div class="table-form-btn-toggle ml10 cursor-pointer" @click="state.isToggle = !state.isToggle">
								<span>{{ state.isToggle ? '收筛选' : '展筛选' }}</span>
								<SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />
							</div>
							<el-button size="default" type="primary" class="ml10" @click="getTableData">
								<el-icon>
									<ele-Search />
								</el-icon>
								查询
							</el-button>
							<el-button size="default" type="primary" class="ml10" @click="onOpen('add', '')">
								<el-icon>
									<ele-FolderAdd />
								</el-icon>
								新增积分商品
							</el-button>
						</el-form-item>
					</el-col>
				</el-row> -->
			</div>
		</div>
		<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
			<!--					<el-table-column prop="title" label="商品名称" show-overflow-tooltip></el-table-column>-->

			<el-table-column label="商品信息" min-width="150">
				<template #default="scope">
					<div class="product-info">
						<el-image
							:src="STATIC_URL + scope.row.image"
							class="product-image"
							fit="cover"
							:preview-src-list="[STATIC_URL + scope.row.image]"
							:initial-index="0"
							preview-teleported
						/>
						<div class="product-details">
							<div class="title sa-table-line-1">{{ scope.row.title }}</div>
							<div class="subtitle sa-table-line-1">{{ scope.row.subTitle }}</div>
							<el-button type="primary" size="small" @click="toggleSpecs(scope.row)" v-if="scope.row.spec == 1" class="goods-more-spec" round>
								多规格
							</el-button>
						</div>
					</div>
				</template>
			</el-table-column>

			<el-table-column label="积分现金" show-overflow-tooltip>
				<template #default="scope"> {{ scope.row.scorePrice.score }}积分+ ￥{{ scope.row.scorePrice.price }} </template>
			</el-table-column>
			<el-table-column prop="sales" label="销量" show-overflow-tooltip></el-table-column>
			<el-table-column prop="stock" label="库存" show-overflow-tooltip></el-table-column>
			<el-table-column prop="updateTime" label="修改时间" show-overflow-tooltip></el-table-column>
			<el-table-column prop="updateUsername" label="操作人" show-overflow-tooltip></el-table-column>
			<el-table-column label="操作" width="100">
				<template #default="scope">
					<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onOpen('edit', scope.row.id)"
						>编辑
					</el-button>
					<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onRowDel(scope.row)">删除 </el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="pagination-container">
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				:page-sizes="[10, 15, 20, 30, 50, 100]"
				v-model:current-page="state.tableData.paging.currentPage"
				background
				v-model:page-size="state.tableData.paging.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.paging.totalCount"
			>
			</el-pagination>
		</div>

		<ScoreDialog ref="ScoreRef" @refresh="onRefresh"></ScoreDialog>

		<GoodsSpecDialog ref="GoodsSpecDialogRef"></GoodsSpecDialog>
	</div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus';
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ScoreApi } from '/@/api/shop/score';
import { resolveStartTime, resolveEndTime } from '/@/views/shop/activity/utils';
import { STATIC_URL } from '/@/utils/config';

const ScoreDialog = defineAsyncComponent(() => import('./dialog.vue'));
const GoodsSpecDialog = defineAsyncComponent(() => import('./dialog_spec.vue'));
const ScoreRef = ref();
const GoodsSpecDialogRef = ref();
const state = reactive({
	isToggle: false,
	search: {
		name: '',
		searchDateTime: '',
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		paging: {
			currentPage: 1,
			pageSize: 15,
			totalCount: 0,
			totalPages: 0,
		},
	},
});

const toggleSpecs = (row: any) => {
	GoodsSpecDialogRef.value.openDialog(row.scoreExchangeGoodsVOS);
};

const onRefresh = () => {
	onHandleCurrentChange(1);
};

const onOpen = (type: string, id: string) => {
	ScoreRef.value.openDialog(type, id);
};

// 分页改变
const onHandleSizeChange = (pageSize: number) => {
	state.tableData.paging.pageSize = pageSize;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
	state.tableData.paging.currentPage = currentPage;
	getTableData();
};

const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除积分名称：“${row.title}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await ScoreApi().delete(row.id);
			getTableData();
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};

const getTableData = async () => {
	const { data } = await ScoreApi().search({
		activityName: state.search.name,
		startTime: resolveStartTime(state.search.searchDateTime),
		endTime: resolveEndTime(state.search.searchDateTime),
		offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
		limit: state.tableData.paging.pageSize,
	});
	state.tableData.data = data.datas;
	state.tableData.paging.currentPage = data.paging.currentPage;
	state.tableData.paging.totalCount = data.paging.totalCount;
	console.log('data: ', data);
};

onMounted(() => {
	getTableData();
});
</script>

<style scoped>
.search {
	background-color: white;
	padding: 0 16px;
}

.table-form-btn-toggle {
	white-space: nowrap;
	user-select: none;
	display: flex;
	align-items: center;
	color: var(--el-color-primary);
}

.product-info {
	display: flex;
	align-items: center;
}

.product-image {
	width: 50px;
	height: 50px;
	object-fit: cover;
	margin-right: 10px;
	border-radius: 4px;
}

.product-details {
	display: flex;
	flex-direction: column;
	font-size: 14px;
}

.title {
	height: 20px;
	line-height: 20px;
	font-size: 14px;
	font-weight: 500;
	color: var(--sd-font);
}

.sa-table-line-1 {
	min-width: 0;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 1;
	-webkit-box-orient: vertical;
	height: 23px;
	line-height: 23px;
}

.sa-goods.goods-scoreShop .subtitle {
	height: 16px;
	line-height: 16px;
	font-size: 12px;
	font-weight: 400;
	color: var(--sd-subfont);
}

.goods-more-spec {
	padding: 5px 5px !important;
}

.system-score-container {
	.el-table {
		flex: 1;
	}
}
</style>
